package com.jboost.modules.app.service.strategry.common;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.system.entity.BaseEntity;
import com.jboost.modules.system.entity.DpDingdan;
import com.jboost.modules.system.entity.FhdBom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.TypeVariable;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public abstract class AbstractJdy <T,V,M extends BaseMapper,E extends BaseEntity> implements Strategy<T,V> {
    @Autowired
    public M baseMapper;

    protected final static String testPrefix = "https://api-sandbox.xiaoman.cn";
    protected final static String prodPrefix= "https://api-sandbox.xiaoman.cn";
    protected final static String client_id = "4265ebaa08192ed4ce490e6cce0f83f0a2cd11f7c7b0b2528a577b0f9016d047e90af60eed77fe38";
    protected final static String client_secret = "85065fe83d7e4733c4d694799c2da89e5a583520ac57da5077fb2e51540a145851864329c8b4c1cb";
    protected final static String tokenUrl = prodPrefix+"/v1/oauth2/access_token";
    protected final static String customersUrl = prodPrefix+"/v1/company/list";
    protected final static String gjdtUrl = prodPrefix+"/v1/dynamic/trail/list";
    protected final static String customerDetailUrl = prodPrefix+"/v1/company/info";

    protected final static String orderUrl = prodPrefix+"/v1/opportunity/list";
    protected final static String orderDetailUrl = prodPrefix+"/v1/opportunity/info";


    public static String getParams(Map<String, Object> params) {
        StringBuffer sb = new StringBuffer("?");
        if (Objects.nonNull(params)) {
            for (Map.Entry<String, Object> item : params.entrySet()) {
                Object value = item.getValue();
                if (Objects.nonNull(value)) {
                    sb.append("&");
                    sb.append(item.getKey());
                    sb.append("=");
                    sb.append(value);
                }
            }
            return sb.toString();
        } else {
            return "";
        }
    }
    public void sync(E entit) throws IOException, ParseException {
        log.info("开始批量查询Hs材料数据");
        baseMapper.delete(Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
        HashMap<String, Object> param = getParam();
        param.put("limit",1000);
        List<JSONObject> jsonObjects = new ArrayList<>();
        do {
            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
            jsonObjects.forEach(e -> {
                try {
                    BaseEntity dpDingdan = JSONObject.parseObject(e.toString(), entit.getClass());
                    Object o = baseMapper.selectById(e.getString("_id"));
                    if (Objects.isNull(o)) {
                        baseMapper.insert(dpDingdan);
                    }else if (!Objects.equals(o,dpDingdan)) {
                        baseMapper.deleteById(e.getString("_id"));
                        baseMapper.insert(dpDingdan);
                    }
                }catch (Exception exception){

                }

            });
            if (jsonObjects.size()>0){

                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
            }
        }while (jsonObjects.size()>0);

    }


    public String jdyUpdate1(String data_id,Object data) throws IOException {
        HashMap<String, Object> param = getParam();
        param.put("entry_id",getFormId1());
        param.put("data_id", data_id);
        param.put("data", data);
        String post = HttpRestUtils.post(update_api , param, getJdyHeaders());
        return post;
    }



    public void run(ApplicationArguments args) throws Exception {
         invokeStrategy();
    }
}
